package com.transaction.seataorder.control;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.transaction.seataorder.annoation.AutoIdempotent;
import com.transaction.seataorder.enums.SexEnum;
import com.transaction.seataorder.model.Order;
import com.transaction.seataorder.server.IOrderServer;
import com.transaction.seataorder.utils.TokenUtil;
import com.transaction.seataorder.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author ausu
 * @version 1.0
 * @description: TODO
 * @date 2022/12/22 19:54
 */
@RestController
@RequestMapping("/order")
public class CreateOrderControl {

    @Autowired(required = false)
    private IOrderServer orderServer;

    @RequestMapping("/create")
    // @RequestBody 只接收body中的json字符串， 要求body中的属性名等于实体中的属性名
    public String create(@RequestBody Order order) {
        System.out.println("接收的参数:::" + order.toString());
        String order1 = orderServer.create(order);

        return order1;
    }

    @Autowired
    public TokenUtil tokenUtil;

    // getToken
    @RequestMapping("/getToken")
    public String getToken() {
        return tokenUtil.createToken();
    }

    /**
     * 幂等处理
     */
    @RequestMapping("/hello")
    @AutoIdempotent
    public String  getOrder() {
        return "hello";
    }

    /**
     * 幂等对比
     */
    @RequestMapping("/hello2")
    public String  getOrde() {
        return "hello2";
    }

    @RequestMapping("/sex")
    public String setSex(@RequestBody Order order) throws Exception {
        System.out.println(order.toString());

        OrderVo orderVo = new OrderVo();
        orderVo.setSex(SexEnum.getValueByKey(order.getSex())).setProductId(order.getProductId());
        return new ObjectMapper().writeValueAsString(orderVo);
    }
}
